﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibUtilities.dal;
using System.Data;
using System.Data.SqlClient;

namespace CLibArtreve.dal
{
    public class SecretQuestionDAL : SqlDatabase
    {
        #region CONSTRUCTORS
        public SecretQuestionDAL(string connectionString) : base(connectionString) { }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "DATA ACCES LAYER: " + this.GetType().ToString();
        }
        #endregion  

        #region GET METHODS
        public DataSet GetAll()
        {
            try
            {
                return ExecuteDataSetWithCommonQuery(Queries.SELECTALLQUESTIONS, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetById(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTQUESTIONBYID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region INSERT METHODS
        public int Insert(string label)
        {
            try
            {
                // MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETERS
                SqlParameter parameter = new SqlParameter();
                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "label";
                parameter.Value = label;
                parameters.Add(parameter);

                // EXECUTE
                int p_id = ExecuteNonQueryWithCommonQuery(Queries.INSERTQUESTION, parameters, true);
                if (p_id > 0)
                {
                    p_id = Convert.ToInt32(ExecuteScalarWithCommonQuery<decimal>(Queries.SELECTIDENTITYAFTERINSERTSQLSERVER));
                }
                return p_id;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region UPDATE METHODS
        public int Update(Int64 id, string label)
        {
            try
            {
                //MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter parameter = new SqlParameter();
                parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "id";
                parameter.Value = id;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "label";
                parameter.Value = label;
                parameters.Add(parameter);

                return ExecuteNonQueryWithCommonQuery(Queries.UPDATEQUESTION, parameters, true);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region DELETE METHODS
        public int Delete(Int64 id)
        {
            try
            {
                //MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                //MAKE PARAMETERS
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "id";
                parameter.Value = id;
                parameters.Add(parameter);

                //EXECUTE
                return ExecuteNonQueryWithCommonQuery(Queries.DELETEQUESTION, parameters, true);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion
    }
}
